package com.wss.common.widget;

import com.wss.common.utils.ImageUtils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.PageSlider;
import ohos.agp.components.TabList;
import ohos.app.Context;
import org.jetbrains.annotations.NotNull;

public class PagerSlidingTabStrip extends TabList {

    private final PageListener pageListener = new PageListener();
    public PageSlider.PageChangedListener delegatePageListener;

    private PageSlider pager;

    private int tabCount;

    private int currentPosition = 0;
    private float currentPositionOffset = 0f;

    public PagerSlidingTabStrip(Context context) {
        this(context,null);
    }

    public PagerSlidingTabStrip(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public PagerSlidingTabStrip(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        addTabSelectedListener(new TabSelectedListener() {
            @Override
            public void onSelected(Tab tab) {
                if (onPagerTabClick != null) {
                    onPagerTabClick.onClick(tab.getPosition());
                }
                if (tab.getPosition() != pager.getCurrentPage()) {
                    pager.setCurrentPage(tab.getPosition(), false);
                }
            }

            @Override
            public void onUnselected(Tab tab) {

            }

            @Override
            public void onReselected(Tab tab) {

            }
        });
    }

    private class PageListener implements PageSlider.PageChangedListener {

        @Override
        public void onPageSliding(int position, float positionOffset, int positionOffsetPixels) {
            currentPosition = position;
            currentPositionOffset = positionOffset;

            if (getTabAt(position) != null) {
//                if (getSelectedTabIndex() != position) {
//                    selectTabAt(position);
//                }
            }

            if (delegatePageListener != null) {
                delegatePageListener.onPageSliding(position,
                        positionOffset,
                        positionOffsetPixels);
            }
        }

        @Override
        public void onPageSlideStateChanged(int state) {
            if (state == PageSlider.SLIDING_STATE_IDLE) {
//                selectTabAt(pager.getCurrentPage());
            }

            if (delegatePageListener != null) {
                delegatePageListener.onPageSlideStateChanged(state);
            }
        }

        @Override
        public void onPageChosen(int position) {
            if (delegatePageListener != null) {
                delegatePageListener.onPageChosen(position);
            }
//            if (getSelectedTabIndex() != position) {
//                selectTabAt(position);
//            }
            if (onViewPagerRefresh != null) {
                onViewPagerRefresh.onRefresh(position);
            }

        }
    }

    private OnViewPagerRefresh onViewPagerRefresh;

    public void setOnViewPagerRefresh(OnViewPagerRefresh onViewPagerRefresh) {
        this.onViewPagerRefresh = onViewPagerRefresh;
    }

    public interface OnViewPagerRefresh {
        void onRefresh(int position);
    }

    private PagerSlidingTabStrip.OnPagerTabClick onPagerTabClick;

    public void setOnPagerTabClick(PagerSlidingTabStrip.OnPagerTabClick onPagerTabClick) {
        this.onPagerTabClick = onPagerTabClick;
    }

    public interface OnPagerTabClick {
        void onClick(int position);
    }

    private void notifyDataSetChanged() {
        if (pager.getProvider() == null) {
            throw new IllegalStateException("PageSlider does not have provider instance.");
        }
        removeAllComponents();
        tabCount = pager.getProvider().getCount();
        for (int i = 0; i < tabCount; i += 1) {
            TabList.Tab tab = new Tab(getContext());
            if (pager.getProvider() instanceof IconTabProvider) {
                tab.setIconElement(ImageUtils.getPixelById(getContext(),
                        ((IconTabProvider) pager.getProvider()).getPageIconResId(i)));
            } else {
                String title = String.valueOf(pager.getProvider().getPageTitle(i));
                tab.setText(title);
            }
            addTab(tab);
        }
        int currentPage = pager.getCurrentPage();
        selectTabAt(currentPage);
        fluentScrollXTo(0);
    }

    /**
     * 添加ViewPager
     *
     * @param pager ViewPager
     */
    public void setViewPager(@NotNull PageSlider pager) {
        this.pager = pager;
        pager.addPageChangedListener(pageListener);
        notifyDataSetChanged();
    }

    /**
     * 设置ViewPager滑动监听
     *
     * @param listener 监听器
     */
    public void setOnPageChangeListener(PageSlider.PageChangedListener listener) {
        this.delegatePageListener = listener;
    }

    /**
     * IconTab 提供则
     */
    public interface IconTabProvider {
        /**
         * 返回Icon资源ID
         *
         * @param position position
         * @return icon资源图片
         */
        int getPageIconResId(int position);
    }
}
